package com.shijiu.controller;

import cn.hutool.core.util.StrUtil;
import com.shijiu.common.ResultTest;
import com.shijiu.params.LoginParam;
import com.shijiu.service.LoginService;
import com.shijiu.common.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * @Author xlj
 * @Date Created in  2023/3/18 14:30
 * @DESCRIPTION:
 * @Version V1.0
 */

@RestController
@RequestMapping("index")
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @PostMapping("login")
    public Result login(@RequestBody LoginParam loginParam){
        return loginService.login(loginParam);
    }

    @ApiOperation("用户登录接收Token")
    @GetMapping("userinfo")
    public Result loginUserInfo(String token){
        log.info("userinfo接口接收到的token:{}",token);
        return loginService.loginUserInfo(token);
    }

    @ApiOperation("用户退出")
    @PostMapping("logout")
    public Result logout(String token,HttpServletRequest httpServletRequest){
        return loginService.logout(token, httpServletRequest);
    }


    @ApiOperation("手机验证码登录接口")
    @PostMapping("/phoneCode")
    @SuppressWarnings("all")
    public ResultTest phoneCodeLogin(@RequestBody @Validated LoginParam loginParam) throws Throwable {
        Optional.ofNullable(loginParam.getPhone()).orElseThrow((Supplier<Throwable>) () -> new RuntimeException("手机号码为null"));
        String phone = loginParam.getPhone();
        log.info("当前获取的手机号为:{}",phone);
        //从redis中获取手机验证码
        String userPhoneKey = redisTemplate.opsForValue().get(phone);
        if (StringUtils.isEmpty(userPhoneKey)) {
            return ResultTest.fail("手机验证码有误");
        }
        if (!userPhoneKey.equals(String.valueOf(loginParam.getCode()))) {
            return ResultTest.fail("手机验证码有误");
        }
        return ResultTest.ok("登录成功");
    }
    @ApiOperation("手机验证码登录接口")
    @PostMapping("/emailCode")
    public ResultTest emailCodeLogin(@RequestBody @Validated  LoginParam loginParam) throws Throwable {
        String email = loginParam.getEmail();
        if (StrUtil.isEmpty(email)){
            throw new Exception("邮箱错误!");
        }
        log.info("当前获取的邮箱为:{}",email);
        //从redis中获取手机验证码
        String emailCode = redisTemplate.opsForValue().get(email);
        if (StringUtils.isEmpty(emailCode)) {
            return ResultTest.fail("邮箱验证码有误");
        }
        if (!emailCode.equals(String.valueOf(loginParam.getCode()))) {
            return ResultTest.fail("邮箱验证码有误");
        }
        return ResultTest.ok("登录成功");
    }

}
